let utils = require('utils');

cc.Class({
    extends: cc.Component,

    properties: {
        father:{
            type:cc.Node,
            default:null
        },
        ao:{
            type:cc.Node,
            default:null
        },
        tu:{
            type:cc.Node,
            default:null
        },
        aoLine:{
            type:cc.Node,
            default:null
        },
        tuLine:{
            type:cc.Node,
            default:null
        },
        previous: {
            type:cc.Node,
            default:null
        },
        next: {
            type:cc.Node,
            default:null
        },
        index:0,
        canDel:false,
        _isDown: false
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._oriPos = this.node.position;
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
        this.node.zIndex = 1;
        if(this.aoLine)this.aoLine.zIndex = 5;
        if(this.tuLine)this.tuLine.zIndex = 5;
    },

    start () {
    },

    // update (dt) {},

    onMouseDown(event) {
        this._isDown = true;
        if (this.father) {
            this.father.getComponent("BlockMoveScript").setMoveBlock(this.node);
        }
    },
    onMouseUp(event) {
        this._isDown = false;
        // cc.log("onMouseUp222");
    },
    _setParent(father, child) {
        utils.node.setParent(father, child, true);
        child.y = -father.height+1;
        child.x = 0;
    },
    attractAo(mb) {
        cc.log("凹块拼接");
        this._setParent(this.node, mb);
        //如果父节点有子节点，把子节点移到新插入的节点下面
        if (this.next) {
            // cc.log(this.next);
            this._setParent(mb, this.next);
            mb.getComponent("BlockScript").next = this.next;
            this.next.getComponent("BlockScript").previous = mb;
        }
        mb.getComponent("BlockScript").previous = this.node;
        this.next = mb;
    },
    attractTu(mb) {
        cc.log("凸块拼接");
        this._setParent(mb, this.node);
        //如果有父节点，把新插入的节点放入中间
        if (this.previous) {
            // cc.log(this.previous);
            this._setParent(this.previous, mb);
            mb.getComponent("BlockScript").previous = this.previous;
            this.previous.getComponent("BlockScript").next = mb;
        }
        mb.getComponent("BlockScript").next = this.node;
        this.previous = mb;
    },
    disattract() {
        utils.node.setParent(this.father, this.node, true);
        if (this.previous) {
            this.previous.getComponent("BlockScript").next = null;
            this.previous = null;
        }
    },
    isChild(node) {
        if (this.next) {
            if (this.next===node) {
                return true;
            }
            if (this.next.getComponent("BlockScript").next===node) {
                return true;
            }
        }
        return false;
    }
});
